﻿#include "AvProtoMsg.h"
#include "AvProtoMsgC.pb-c.h"

int _MsgConnect2Net(char* data, CMsgHead* MsgHead, C_Connect* Connect)
{
	AvProtoMsg__MsgConnect _Msg;
	char msgBuf[256] = { 0 };
	__av_proto_msg____msg_connect__init(&_Msg);
	_Msg.uid = Connect->UID;
	_Msg.uuid = Connect->UUID;
	_Msg.macaddr = Connect->MacAddr;
	_Msg.timeval_tvsec = 0;
	_Msg.timeval_tvusec = 0;
	AppRunMSec(_Msg.looptimestamp);
	int len = __av_proto_msg____msg_connect__pack(&_Msg, msgBuf);

	Zero(MsgHead);
	SetCommand(MsgHead, _ukProtoRequest | __MsgConnect);
	SetPayLoad(MsgHead, len);
	char* pHead = Serialize(MsgHead);
	int headLength = ByteSize(MsgHead);
	memcpy(data, pHead, headLength);
	memcpy(data + headLength, msgBuf, len);

	return len + headLength;
}
int _MsgConnect2Local(char* data, int datalen, C_Connect* Connect)
{
	AvProtoMsg__MsgConnect* _pMsg = __av_proto_msg____msg_connect__unpack(NULL, datalen, data);
	if (NULL == _pMsg) {
		return -1;
	}
	else {
		sprintf(Connect->UID, "%s", _pMsg->uid);
		sprintf(Connect->UUID, "%s", _pMsg->uuid);
		sprintf(Connect->MacAddr, "%s", _pMsg->macaddr);
		Connect->TimeVal_TvSec = (unsigned long)_pMsg->timeval_tvsec;
		Connect->TimeVal_TvUsec = (unsigned long)_pMsg->timeval_tvusec;
		Connect->LoopTimeStamp = (unsigned long)_pMsg->looptimestamp;
	}
	__av_proto_msg____msg_connect__free_unpacked(_pMsg, NULL);
	return 0;
}
int _MsgKeepAlive2Net(char* data, CMsgHead* MsgHead, C_KeepAlive* KeepAlive)
{
	AvProtoMsg__MsgKeepAlive _Msg;
	char msgBuf[512] = { 0 };
	__av_proto_msg____msg_keep_alive__init(&_Msg);
	_Msg.systemuptime = KeepAlive->SystemUpTime;

	int len = __av_proto_msg____msg_keep_alive__pack(&_Msg, msgBuf);
	Zero(MsgHead);
	SetCommand(MsgHead, _ukProtoRequest | __MsgKeepAlive);
	SetPayLoad(MsgHead, len);
	char* pHead = Serialize(MsgHead);
	int headLength = ByteSize(MsgHead);
	memcpy(data, pHead, headLength);
	memcpy(data + headLength, msgBuf, len);
	return len + headLength;
}
int _MsgKeepAlive2Local(CMsgHead* MsgHead, C_KeepAlive* KeepAlive)
{
	return 0;
}
int _MsgDevLogIteam2Net(char* data, CMsgHead* MsgHead, LogItem* iteam)
{
	AvProtoMsg__MsgDevLogItem _Msg;
	char msgBuf[512] = { 0 };
	__av_proto_msg____msg_dev_log_item__init(&_Msg);
	_Msg.fileline = iteam->FileLine;
	_Msg.filename = iteam->FileName;
	_Msg.type = iteam->Type;
	_Msg.timeval_tvsec = iteam->Timeval_tvSec;
	_Msg.timeval_tvusec = iteam->Timeval_tvUsec;
	_Msg.uptime = iteam->UpTime;

	if (_Msg.timeval_tvsec == 0) {
		unsigned long _AppRun = 0;
		AppRunMSec(_AppRun);
		_Msg.timeval_tvsec = time(NULL) - (_AppRun - iteam->UpTime) / 1000;
		_Msg.timeval_tvusec = (iteam->UpTime % 1000) * 1000;
	}
	_Msg.msg = iteam->Msg;
	int len = __av_proto_msg____msg_dev_log_item__pack(&_Msg, msgBuf);

	Zero(MsgHead);
	SetCommand(MsgHead, _ukProtoRequest | __MsgDevLog);
	SetPayLoad(MsgHead, len);
	char* pHead = Serialize(MsgHead);
	int headLength = ByteSize(MsgHead);
	memcpy(data, pHead, headLength);
	memcpy(data + headLength, msgBuf, len);
	return len + headLength;

}

int _MsgSystemVersion2Net(char* data, CMsgHead* MsgHead, C_SystemVersion* SystemVersion)
{
	AvProtoMsg__MsgSystemVersion _Msg;
	char msgBuf[512] = { 0 };
	__av_proto_msg____msg_system_version__init(&_Msg);
	_Msg.appversion = SystemVersion->AppVersion;
	_Msg.appversionstring = SystemVersion->AppVersionString;
	int len = __av_proto_msg____msg_system_version__pack(&_Msg, msgBuf);

	Zero(MsgHead);
	SetCommand(MsgHead, _ukProtoRespond | __MsgSystemVersion);
	SetPayLoad(MsgHead, len);
	char* phEAD = Serialize(MsgHead);
	int headLength = ByteSize(MsgHead);
	memcpy(data, phEAD, headLength);
	memcpy(data + headLength, msgBuf, len);
	return len + headLength;
}
